package com.skaura.cryserver.code;

import com.skaura.cryserver.model.MessageWapper;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;

public class CustomDecoder extends LengthFieldBasedFrameDecoder {
    public static final  int  HEADER_SIZE = 4; // type + len = 2 + 2 = 4

    /**
     *
     * @param maxFrameLength 解码时，处理每个帧数据的最大长度
     * @param lengthFieldOffset 该帧数据中，存放该帧数据的长度的数据的起始位置
     * @param lengthFieldLength 记录该帧数据长度的字段本身的长度
     * @param lengthAdjustment 修改帧数据长度字段中定义的值，可以为负数
     * @param initialBytesToStrip 解析的时候需要跳过的字节数
     * @param failFast 为true，当frame长度超过maxFrameLength时立即报TooLongFrameException异常，为false，读取完整个帧再报异常
     */
    public CustomDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength, int lengthAdjustment, int initialBytesToStrip, boolean failFast) {
        super(maxFrameLength, lengthFieldOffset, lengthFieldLength, lengthAdjustment, initialBytesToStrip, failFast);
    }

    @Override
    protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
        if (in == null) {
            return null;
        }
        if (in.readableBytes()<HEADER_SIZE){
            throw new Exception("readable size less then HEADER_SIZE");
        }
        MessageWapper messageWapper = new MessageWapper();
        messageWapper.setType(in.readShort());
        messageWapper.setLen(in.readShort());
        if (in.readableBytes() < messageWapper.getLen()) {
            throw new Exception("readale size less then len");
        }
        ByteBuf byteBuf = in.readBytes(messageWapper.getLen());
        byte[] bytes = new byte[byteBuf.readableBytes()];
        byteBuf.readBytes(bytes);
        messageWapper.setBody(new String(bytes,"utf-8"));
        return messageWapper;
    }


}
